package com.ygo.feihua.adapter;

import android.content.*;
import android.support.v7.widget.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import cn.bmob.v3.datatype.*;
import cn.bmob.v3.exception.*;
import cn.bmob.v3.listener.*;
import com.xiaomi.push.service.*;
import com.ygo.feihua.*;
import com.ygo.feihua.BmobTable.*;
import java.util.*;
import com.ygo.feihua.Management.*;
import com.ygo.feihua.util.*;
import cn.bmob.v3.*;

public class ZixunAdapter extends RecyclerView.Adapter<ZixunAdapter.ViewHolder>
{

	
	

	private List<ZiXun> data;
	private Context context;
	private Gongju gj;

	private DialogUtils du;
	
	 
	public ZixunAdapter(Context context,List<ZiXun> data){
		this.data=data;
		this.context=context;
		gj=Gongju.getdx(context);
		du=DialogUtils.getdx(context);
		
	}
	@Override
	public ZixunAdapter.ViewHolder onCreateViewHolder(ViewGroup p1, int p2)
	{
		View v=LayoutInflater.from(p1.getContext()).inflate(R.layout.item_zixun,p1,false);
		
		// TODO: Implement this method
		return new ViewHolder(v);
	}

	@Override
	public void onBindViewHolder(final ZixunAdapter.ViewHolder vh, final int position)
	{
		final ZiXun zixun=data.get(position);
		vh.zx_title.setText(zixun.getZs_title());
		vh.zx_time.setText(zixun.getCreatedAt());
		List<BmobFile> bf=zixun.getZs_message().getImage();
		if (bf != null){
			vh.zx_tp.setVisibility(View.VISIBLE);
			gj.tuxian(bf.get(0).getFileUrl(), vh.zx_tp);
		}else{
			List<TieziMessage> tm=zixun.getZs_message().getMessage();
			if (tm != null && tm.size() != 0){
				vh.zx_tp.setVisibility(View.VISIBLE);
				boolean bb=false;
				for (TieziMessage tt:tm){
					if (tt.getImage() != null){
						bb = true;
						gj.tuxian(tt.getImage().getFileUrl(), vh.zx_tp);
						break;
					}
				}
				if (!bb){
					vh.zx_tp.setVisibility(View.GONE);
				}
			}else{
				vh.zx_tp.setVisibility(View.GONE);
			}
		}
		
		vh.v.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					String id=zixun.getZs_message().getObjectId();
					if(id==null){
						gj.show("该资讯已被删除");
					}else{
						gj.tieziIntent(id);
					}
					// TODO: Implement this method
				}
			});
		vh.v.setOnLongClickListener(new OnLongClickListener(){

				@Override
				public boolean onLongClick(View p1)
				{
					ItemLongClick(vh.v,position);
					// TODO: Implement this method
					return true;
				}
			});
		
		// TODO: Implement this method
	}

	private void ItemLongClick(View v,final int position)
	{
		UserManagement um=UserManagement.getDx();
		
		if(um.getUser()!=null&& um.getUser().getUser_qx()==1){
			ListView ll=(ListView) du.dialogl(true,"修改资讯信息",new String[]{"修改标题","修改关联帖子","删除资讯"});
			ll.setOnItemClickListener(new AdapterView.OnItemClickListener(){

					@Override
					public void onItemClick(AdapterView<?> p1, View p2, final int p3, long p4)
					{

						switch(p3){
							case 0:
								du.dis();
								View[] v=du.dialoge(false,"","修改标题");
								final EditText ed=(EditText) v[0];
								Button bt=(Button) v[1];

								bt.setOnClickListener(new OnClickListener(){

										@Override
										public void onClick(View p1)
										{
											String bt=ed.getText().toString().trim();
											if(bt.equals("")){
												gj.show("标题不能为空");

											}else{
												du.dis();
												du.dialogj1(false,"","更新中,请稍等");
												ZiXun zx=new ZiXun();
												zx.setZs_title(bt);
												zx.update(data.get(position).getObjectId(), new UpdateListener(){

														@Override
														public void done(BmobException p1)
														{
															if(p1==null){
																du.dis();
																gj.show("更新成功");

															}else{
																du.dis();
																gj.show("更新失败,原因为"+p1);
															}


															// TODO: Implement this method
														}
													});
											}

											// TODO: Implement this method
										}
									});

								break;
							case 1:
								du.dis();
								updateimage(position);
								break;
							case 2:
								ZiXun zzx=new ZiXun();
								zzx.delete(data.get(position).getObjectId(), new UpdateListener(){

										@Override
										public void done(BmobException p1)
										{
											if(p1==null){
												gj.show("删除成功");
												notifyDataSetChanged();
											}else{
												gj.show("删除失败,原因为"+p1);
											}
											du.dis();
											// TODO: Implement this method
										}
									});
								break;
						}

						// TODO: Implement this method
					}

				});
		}
		// TODO: Implement this method
	}

	@Override
	public int getItemCount()
	{
		// TODO: Implement this method
		return data.size();
	}
	
	class ViewHolder extends RecyclerView.ViewHolder{
		View v;
		TextView zx_title,zx_time;
		ImageView zx_tp;

		
		public ViewHolder(View v){
			super(v);
			this.v=v;
			zx_title=(TextView) v.findViewById(R.id.zx_title);
			zx_time=(TextView) v.findViewById(R.id.zx_time);
			zx_tp=(ImageView) v.findViewById(R.id.zx_tp);
		}
	}
	
	private void updateimage(final int gs){
		BmobQuery<Tiezi> bq=new BmobQuery<Tiezi>();
		bq.order("createdAt");
		bq.findObjects(new FindListener<Tiezi>(){

				@Override
				public void done(final List<Tiezi> tie, BmobException p2)
				{
					if(p2==null){
						String[] ss=new String[tie.size()];
						for(int i=0;i<tie.size();i++){
							ss[i]=tie.get(i).gettitle();

						}
						ListView list=(ListView) du.dialogl(true,"请选择资讯帖子",ss);

						list.setOnItemClickListener(new AdapterView.OnItemClickListener(){

								@Override
								public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
								{
									du.dis();
									du.dialogj1(false,"","更新中,请稍等");

									ZiXun zxx=new ZiXun();
									zxx.setZs_message(tie.get(p3));
									zxx.update(data.get(gs).getObjectId(), new UpdateListener(){

											@Override
											public void done(BmobException p1)
											{
												if(p1==null){
													gj.show("更新成功");
												}else{
													gj.show("更新失败,原因为"+p1);
												}
												du.dis();
												// TODO: Implement this method
											}
										});

									// TODO: Implement this method
								}
							});


					}else{
						gj.show("查询图片失败,原因为"+p2);
					}
					// TODO: Implement this method
				}
			});

	}

	
	
}
